package com.truyentranh.main;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.SessionStore;
import com.facebook.android.Utility;
import com.truyentranh.R;
import com.truyentranh.controls.AdsViewControl;
import com.truyentranh.data.HaiBLModule;
import com.truyentranh.data.TipData;
import com.truyentranh.modules.cabvn.CabVnActivity;
import com.truyentranh.modules.epicvn.EpicVnActivity;
import com.truyentranh.modules.girl.GirlActivity;
import com.truyentranh.modules.haivl.HaiVLActivity;
import com.truyentranh.modules.local.MoreActivity;
import com.truyentranh.modules.local.UtilsWebActivity;
import com.truyentranh.modules.news.NewsActivity;
import com.truyentranh.modules.ohvui.OhVuiActivity;
import com.truyentranh.modules.tamdiem.TamDiemActivity;
import com.truyentranh.modules.truyentranh.TruyenTranhBookmarkActivity;
import com.truyentranh.modules.truyentranh.TruyenTranhCategoryActivity;
import com.truyentranh.network.APIRequestWorker;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.requester.UpdateRequester;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;
import com.truyentranh.utils.sqllite.datasource.TruyenTranhBookmark;

public class MainMenuHaiBLActivity extends Activity implements OnClickListener {

	public static final String APP_ID = "354042864672977";// "157111564357680";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Logger.logMe("onCreateonCreateonCreateonCreate");
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.act_mainmenu);

		AppSettingParams.SCREEN_WIDTH = getWindowManager().getDefaultDisplay().getWidth();
		AppSettingParams.SCREEN_HEIGHT = getWindowManager().getDefaultDisplay().getHeight();

		// Create the Facebook Object using the app id.
		Utility.mFacebook = new Facebook(APP_ID);
		// Instantiate the asynrunner object for asynchronous api calls.
		Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);

		SessionStore.restore(Utility.mFacebook, this);

		FrameLayout layoutTruyenTranh = (FrameLayout) findViewById(R.id.layoutTruyenTranh);
		FrameLayout layoutBookmark = (FrameLayout) findViewById(R.id.layoutBookmark);
		ImageView loadGooglePlay = (ImageView) findViewById(R.id.loadGooglePlay);

		layoutTruyenTranh.setOnClickListener(this);
		layoutBookmark.setOnClickListener(this);
		loadGooglePlay.setOnClickListener(this);
	}

	@Override
	protected void onStart() {

		if (!AdsViewControl.NO_ADS) {
			AppSettingParams.initPublishId(MainMenuHaiBLActivity.this);
			doCallUpdatesVersion();
		}
		AppSettingParams.initSession(this);
		AppSettingParams.initFaceBookAppId(MainMenuHaiBLActivity.this);

		super.onStart();
	};

	@Override
	public void onClick(View v) {
		Intent intent = null;
		if (v != null) {
			int id = v.getId();
			switch (id) {
			case R.id.loadGooglePlay:
				String url = "https://play.google.com/store/apps/details?id=com.haitonghop";
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				startActivity(browserIntent);
				
				
				break;
			case R.id.layoutTruyenTranh:
				doShow(HaiBLModule.COMIC);
				break;
			case R.id.layoutBookmark:
				doShow(HaiBLModule.BOOKMARK);
				break;
			default:
				break;
			}
		}
	}

	public static boolean isCall = false;

	void doCallUpdatesVersion() {
		if (isCall)
			return;
		isCall = true;
		UpdateRequester updateRequester = new UpdateRequester(new RequestListener() {

			@Override
			public void onRequestFailed(String errorMsg, Request request) {

			}

			@Override
			public void onRequestComplete(Request request) {
				if (request instanceof UpdateRequester) {
					final UpdateRequester r = (UpdateRequester) request;
					if (r.getVersion() > AppSettingParams.VERSION_CODE) {
						final String message = r.getMessage();
						final String title = "Cập nhật" + (r.isForce() ? " bắt buộc" : "");
						final String link = r.getLink();
						// has new updates for app

						final AlertDialog.Builder alert = DisplayUtils.showUpdatesDialog(r.isForce(), title, message, MainMenuHaiBLActivity.this, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								finish();

								Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
								startActivity(browserIntent);
							}
						}, new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								if (r.isForce()) {
									finish();
								}
							}
						}, null);

						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								try {
									alert.show();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
					} else {
						System.out.println("show tip");
						// show tip len
						final TipData data = r.getTipdata();
						if (data != null) {
							System.out.println("show tip");
							runOnUiThread(new Runnable() {
								@Override
								public void run() {
									DisplayUtils.openTipDialog("Chú ý", data.getMesage(), data.getLink(), MainMenuHaiBLActivity.this);
								}
							});

						}
					}
				}

			}
		});

		APIRequestWorker.getInstance().addAPIRequest(updateRequester);
	}

	public void doShow(String id) {
		Log.d("doShow", "" + id);
		if (id != null) {
			if (HaiBLModule.HAIVL.equals(id)) {
				Intent intent = new Intent(getApplication(), HaiVLActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.CABVN.equals(id)) {
				Intent intent = new Intent(getApplication(), CabVnActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.OHVUI.equals(id)) {
				Intent intent = new Intent(getApplication(), OhVuiActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.TAMDIEM.equals(id)) {
				Intent intent = new Intent(getApplication(), TamDiemActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.QUAYBAO.equals(id)) {
				Intent intent = new Intent(getApplication(), NewsActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.GIRLXINH.equals(id)) {
				Intent intent = new Intent(getApplication(), GirlActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.COMIC.equals(id)) {
				Intent intent = new Intent(getApplication(), TruyenTranhCategoryActivity.class);
				startActivity(intent);
			} else if (HaiBLModule.BOOKMARK.equals(id)) {
				Intent intent = new Intent(getApplication(), TruyenTranhBookmarkActivity.class);
				startActivity(intent);
			} else {
				Logger.logMe(">>>>>> id - " + id);
				Intent intent = new Intent(getApplication(), MoreActivity.class);
				startActivity(intent);
			}
		}
	}

}
